home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Digital Signatures / Digital Signature Demo / Source ƒ / CSignature.c next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  6.3 KB  |  275 lines  |  [TEXT/KAHL]

  1. /*
  2.  * CSignature.c
  3.  * Copyright © 1993 Apple Computer Inc.
  4.  * All Rights Reserved
  5.  *
  6.  * This is a Think C class library definition that permits
  7.  * any Think C object to have an attached digital signature.
  8.  * Methods allow you to attach, save, restore, and verify
  9.  * signatures.
  10.  *
  11.  * CSignature is a base class with two sub-classes,
  12.  * CSignedDataFile and CSignedObject. The base class
  13.  * contains methods that are needed for all Digital
  14.  * Signature processing.
  15.  *
  16.  * CSignedDataFile signs entire files. It would normally
  17.  * be called from a CDocument class.
  18.  *
  19.  * CSignedObject signs individual objexts. It might be
  20.  * called from, perhaps, a spreadsheet that needs to
  21.  * sign the contents of individual cells. 
  22.  *
  23.  * Note: errors are returned through the Think Class
  24.  * Library "Failure" routines.
  25.  */
  26. #include "CSignature.h"
  27. #include "SIGStatusManager.h"
  28. #include <OCEErrors.h>
  29. #include <OCE.h>
  30. #include <DigitalSignature.h>
  31. #include <Exceptions.h>
  32. #include <Global.h>
  33.  
  34. /*
  35.  * This is the only instance of a signature context pointer.
  36.  */
  37. SIGContextPtr            gSIGContextPtr;
  38.  
  39. /*
  40.  * ISignature is called when the object is created. Note
  41.  * that it succeeds even if the Digital Signature Manager
  42.  * is not available. It is the application's responsibility
  43.  * to avoid calling any methods that call Digital Signature
  44.  * Toolbox functions.
  45.  */
  46. void
  47. CSignature::ISignature(void)
  48. {
  49.         itsStatusManager = NULL;
  50. }
  51.  
  52. /*
  53.  * Dispose is called when the object is destroyed.
  54.  */
  55. void
  56. CSignature::Dispose(void)
  57. {
  58.         DisposeDefaultStatusProc();
  59.         ForgetObject(itsStatusManager);
  60.         DisposeSignerContext();
  61.         inherited::Dispose();
  62. }
  63.  
  64. /*
  65.  * Create a signature context. ContextType should be one of the
  66.  * following values. It is needed for UpdateMenus and similar
  67.  * to enable/disable the ShowSigner command.
  68.  * kSIGSign            Signing
  69.  * kSIGVerify        Verify
  70.  * kSIGDigest        Digest
  71.  */
  72. void
  73. CSignature::NewContext(
  74.         unsigned long        contextType
  75.     )
  76. {        
  77.         DisposeSignerContext();
  78.         FailOSErr(SIGNewContext(&gSIGContextPtr));
  79.         FailNIL(gSIGContextPtr);
  80.         itsContextType = contextType;
  81. }
  82.  
  83. /*
  84.  * Fail if there is no context.
  85.  *    kSIGContextPrepareErr            There is no current context.
  86.  */
  87. void
  88. CSignature::CheckForContext(void)
  89. {
  90.         if (gSIGContextPtr == NULL)
  91.             FailOSErr(kSIGContextPrepareErr);
  92. }
  93.  
  94. /*
  95.  * Return the context type, or zero if there is no context.
  96.  */
  97. unsigned long
  98. CSignature::GetContextType(void)
  99. {
  100.         if (gSIGContextPtr == NULL)
  101.             return (0);
  102.         else {
  103.             return (itsContextType);
  104.         }
  105. }
  106.  
  107. /*
  108.  * SignPrepare creates a new signature context and either
  109.  * reads a specified signature file or prompts the user
  110.  * for a signature. All errors (including userCanceled)
  111.  * exit via Failure. On success, it returns the
  112.  * size of the signature record. The parameters are as follows:
  113.  *
  114.  * signerFile        If not NULL, this is the file containing
  115.  *                    a user's signature file. If NULL, the
  116.  *                    method will prompt to let the user
  117.  *                    select a signature file.
  118.  * prompt            Normally "\p", this may be set to a
  119.  *                    user-specified prompt string.
  120.  */
  121. Size
  122. CSignature::SignPrepare(
  123.         const FSSpec                *signerFile,
  124.         ConstStr255Param            prompt
  125.     )
  126. {
  127.         Size                        signatureSize;
  128.  
  129.         NewContext(kSIGSign);
  130.         TRY {
  131.             FailOSErr(SIGSignPrepare(
  132.                 gSIGContextPtr,
  133.                 signerFile,
  134.                 prompt,
  135.                 &signatureSize
  136.             ));
  137.         }
  138.         CATCH {
  139.             DisposeSignerContext();
  140.         }
  141.         ENDTRY;
  142.         return (signatureSize);
  143. }
  144.  
  145. /*
  146.  * After verifying a signature, an application can call
  147.  * ShowSigner to display the certificate information.
  148.  */
  149. void
  150. CSignature::ShowSigner(
  151.         ConstStr255Param            prompt
  152.     )
  153. {
  154.         CheckForContext();
  155.         InitCursor();
  156.         FailOSErr(SIGShowSigner(gSIGContextPtr, prompt));
  157. }
  158.         
  159. /*
  160.  * GetSignerInfo copies information about the signer
  161.  * to a user-provided buffer. You may call this
  162.  * after you have signed or verified a file, and
  163.  * before you have disposed of the signature context.
  164.  *        (Untested)
  165.  */
  166. void
  167. CSignature::GetSignerInfo(
  168.         SIGSignerInfo            *signerInfo
  169.     )
  170. {
  171.         CheckForContext();
  172.         FailOSErr(SIGGetSignerInfo(gSIGContextPtr, signerInfo));
  173. }
  174.  
  175. /*
  176.  * Get information about one of the certificates in the signature
  177.  * certificate chain. On TRUE return, the information has been stored
  178.  * into the user's buffer. On FALSE return, the certificate index
  179.  * parameter is out of range. Other errors exit through the failure
  180.  * mechanism.
  181.  *        (Untested)
  182.  */
  183. Boolean
  184. CSignature::GetCertInfo(
  185.         unsigned long                certIndex,
  186.         SIGCertInfo                    *certInfo
  187.     )
  188. {
  189.         OSErr                status;
  190.         
  191.         CheckForContext();
  192.         status = SIGGetCertInfo(gSIGContextPtr, certIndex, certInfo);
  193.         switch (status) {
  194.         case noErr:                return (TRUE);
  195.         case kSIGIndexErr:        return (FALSE);
  196.         default:                FailOSErr(status);
  197.         }
  198. }
  199.  
  200. /*
  201.  * Get information about a specific attribute of a distinguished name
  202.  * in a specific certificate of a signature.  Returns TRUE if
  203.  * successful, FALSE if an index was outside the allowable range.
  204.  * Fail on other errors
  205.  *        (Untested)
  206.  */
  207. Boolean
  208. CSignature::GetCertNameAttributes(
  209.         unsigned long                certIndex,
  210.         unsigned long                attributeIndex,
  211.         SIGNameAttributesInfo        *attributeInfo
  212.     )
  213. {
  214.         OSErr                status;
  215.         
  216.         CheckForContext();
  217.         status = SIGGetCertNameAttributes(
  218.                     gSIGContextPtr,
  219.                     certIndex,
  220.                     attributeIndex,
  221.                     attributeInfo
  222.                 );
  223.         switch (status) {
  224.         case noErr:                return (TRUE);
  225.         case kSIGIndexErr:        return (FALSE);
  226.         default:                FailOSErr(status);
  227.         }
  228. }
  229.  
  230. /*
  231.  * Setup for the status window callback.
  232.  */
  233. void
  234. CSignature::InitDefaultStatusProc(
  235.             ConstStr255Param            actionString,
  236.             ConstStr255Param            objectString
  237.     )
  238. {
  239.         ForgetObject(itsStatusManager);
  240.         itsStatusManager = new (SIGStatusManager);
  241.         itsStatusManager->ISIGStatusManager(actionString, objectString);
  242. }
  243.  
  244. void
  245. CSignature::DisposeDefaultStatusProc(void)
  246. {
  247.         ForgetObject(itsStatusManager);
  248. }
  249.  
  250. /*
  251.  *** The following is a function, not a method.
  252.  */
  253. /*
  254.  * DisposeSignerContext frees memory needed by a signature context,
  255.  * and frees up memory needed by the Digital Signature manager. You
  256.  * should do this as soon as possible after signing or verifying a
  257.  * file or object. The functions delete context on error, but retain
  258.  * it (so you can extract information about the signer or certificate
  259.  * after successful returns).
  260.  *
  261.  * Note: this is a global function. We ignore any erros.
  262.  */
  263. void
  264. DisposeSignerContext(void)
  265. {
  266.         SIGContextPtr            aContextPtr;
  267.         
  268.         if ((aContextPtr = gSIGContextPtr) != NULL) {
  269.             gSIGContextPtr = NULL;
  270.             (void) SIGDisposeContext(aContextPtr);
  271.         }
  272. }
  273.  
  274.  
  275.